///**
// * Alipay.com Inc.
// * Copyright (c) 2004-2021 All Rights Reserved.
// */
//package com.pubinfo.passbook.tax;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.alipay.mychain.gov.sdk.GovDataSDK;
//import com.alipay.mychain.gov.sdk.base.*;
//import com.alipay.mychain.gov.sdk.base.model.BaseInvoiceContent;
//import com.alipay.mychain.gov.sdk.facade.enums.EncryptTpyeEnum;
//import com.alipay.mychain.gov.sdk.request.SignedRequest;
//import com.pubinfo.passbook.config.ApplicationConfig;
//import com.pubinfo.passbook.entity.TSourceInvoice;
//import lombok.extern.slf4j.Slf4j;
//import okhttp3.*;
//import org.apache.http.entity.ContentType;
//import org.bouncycastle.operator.OperatorCreationException;
//import org.bouncycastle.pkcs.PKCSException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.util.CollectionUtils;
//
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * @author gulidong
// * @version : TestJ.java, v 0.1 2021年03月25日 17:11 gulidong Exp $
// */
//@Slf4j
//public class Demo {
//
//  private static final String instCode = "91330000744141180B";
//  private static final String password = "FmrhvvLrkYkIdST9";//填私钥秘钥，password
//  private static final String privateKeyContent = "-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIH6MFUGCSqGSIb3DQEFDTBIMCcGCSqGSIb3DQEFDDAaBBS16RRrXkqcmuQfX9DA\nTymRpR7nzQICCAAwHQYJYIZIAWUDBAEqBBAm+xlm5x3El31iwpZIh8GeBIGg9/B1\nIdMbcP+HrIudHPSIdmPd9uPn+E9cwgymz+cNTuGrOfW5Tw9BQOlW4NKfCg9zWjrh\nVUs62KCy+Nj9nAqHUnjmSKlDPyQqCOUpQIoeIuf7Lr12XwXMtdoydS8ctKcOZVuD\nA8/s/EcLZvgtMNDK98fl8bSwaHiG4jB4xzBH0n5jsVM5WWe+1fzq0HBdzga/ZT1O\nYEepJWZkgzM/Gdb2Aw==\n-----END ENCRYPTED PRIVATE KEY-----\n";//填秘钥，pkcs8PrivateKey
//  @Autowired
//  private ApplicationConfig config;
//
//  public static void main(String[] arg) throws PKCSException, OperatorCreationException, IOException {
//    OkHttpClient client = new OkHttpClient().newBuilder()
//        .build();
//    MediaType mediaType = MediaType.parse("application/json");
//    RequestBody body = RequestBody.create(mediaType, "{\n  \"einvoiceType\": \"19\",\n  \"invoiceCode\": \"33010121\",\n  \"invoiceNumber\": \"3322057268\"\n}");
//    Request request = new Request.Builder()
//        .url("http://30dz845362.wicp.vip/api/passbook/detailsByParam")
//        .method("POST", body)
//        .addHeader("User-Agent", "apifox/1.0.0 (https://www.apifox.cn)")
//        .addHeader("Content-Type", "application/json")
//        .build();
//    Response response = client.newCall(request).execute();
//
//    System.out.println(response.body().string());
////    String uuid = UUID.randomUUID().toString();
////    //log.info(Util.getTaxOnTheDay("2021-5-1", "2021-7-1").toString());
////    //单日发票总数查询&分页获取
////    queryByTime(uuid, "91330000744141180B", "2021-07-29", "1");
////
////    fetchPage(uuid, "91330000744141180B", 1);
//    // 单张发票验真
//    //   verify("91440300795421713J","4403204130","12231854","2021-03-11","351281.1");
//
//    // 发票状态回写
//    //    booking("91330106MA2AYNWD8M","011002000711","32050505","ZZS_DZFP","Y","Y","A","B","202101","Y","Y","91110108MA01G0FB09");
//
//  }
//
//  private static void fetchPage(String requestId, String nsrsbh, int pageNumber) {
//    String jsonStr = "{\"nsrsbh\":\"" + nsrsbh + "\",\"pageNumber\":" + pageNumber + ",\"requestId\":\"" + requestId + "\"}";
//
//    String signStr = sign(jsonStr);
//    System.err.println(signStr);
//    List<TSourceInvoice> pageResult = postAndDecrypt(sign(jsonStr), "https://einvoice.cloud.alipay.com/api/invoice/fetchPage");
//    System.out.println(pageResult);
//  }
//
//  private static TSourceInvoice toTInvoice(TrustedInvoiceContent encryptedDataAndSignDatum, DecryptAndVerifyUtils decryptAndVerifyUtils) {
//    BaseInvoiceContent baseInvoiceContent = decryptAndVerifyUtils.parse(new String(encryptedDataAndSignDatum.getInvoiceContent(), StandardCharsets.UTF_8));
//    TSourceInvoice invoice = new TSourceInvoice(baseInvoiceContent, encryptedDataAndSignDatum);
//    boolean verify = decryptAndVerifyUtils.verify(encryptedDataAndSignDatum.getTrustedData());
//    if (verify) {
//      invoice.setStatus(0);
//    } else {
//      invoice.setStatus(1);
//    }
//    return invoice;
//  }
//
//  // 密文获取，证书验证，明文处理
//  public static List<TSourceInvoice> postAndDecrypt(String jsonStr, String uri) {
//    // 获取密文和签名集合
//    String response = null;
//    long current = 0;
//    try {
//      current = System.currentTimeMillis();
//      response = HttpClientUtils.doPost(uri, jsonStr, ContentType.APPLICATION_JSON);
//
//    } catch (Exception e) {
//      e.printStackTrace();
//    } finally {
//      current = System.currentTimeMillis() - current;
//      System.out.println("耗时：" + current);
//    }
//
//    //获取明文和签名
//    DecryptAndVerifyUtils decryptAndVerifyUtils = new DecryptAndVerifyUtils(instCode, password, privateKeyContent);
//    GenericResult result = JSON.parseObject(response, GenericResult.class);
//    // System.out.println(result);
//    PageResult pageResult = JSONObject.parseObject(result.getData(), PageResult.class);
//    List<TrustedInvoiceContent> encryptedDataAndSignData = decryptAndVerifyUtils.getEncryptedDataAndSignData(pageResult);
//    BaseInvoiceContent parse;
//    // 证书验证 & 明文操作
//
//    // 证书验证 & 明文操作
//    List<TSourceInvoice> invoiceList = new ArrayList<>();
//    if (!CollectionUtils.isEmpty(encryptedDataAndSignData)) {
//      for (TrustedInvoiceContent encryptedDataAndSignDatum : encryptedDataAndSignData) {
//        TSourceInvoice invoice = toTInvoice(encryptedDataAndSignDatum, decryptAndVerifyUtils);
//
//        invoiceList.add(invoice);
//      }
//    }
//    System.out.println("耗时2：" + current);
//    return invoiceList;
//  }
//
//  public static String sign(String jsonStr) {
//
//    GovDataSDK.getInstance().init(instCode, privateKeyContent, password, EncryptTpyeEnum.SM4);
//    SignedRequest request1 = GovDataSDK.getInstance().getRequestBuilder().generateSignedPayload(jsonStr);
//    return JSON.toJSONString(request1);
//  }
//
//  private static void queryByTime(String requestId, String nsrsbh, String kprq, String gx_falg) {
//    JSONObject jsonObject = new JSONObject();
//    jsonObject.put("authorizedNsrsbh", nsrsbh);
//    jsonObject.put("nsrsbh", nsrsbh);
//    jsonObject.put("kprq", kprq);
//    jsonObject.put("gxfbs", gx_falg);
//    jsonObject.put("requestId", requestId);
//    // String jsonStr  = "{\"authorizedNsrsbh\":\"" + nsrsbh + "\",\"nsrsbh\":\"" + nsrsbh  "\",\"kprq\":\"" + kprq + "\",\"gxfbs\":" + gx_falg + ",\"requestId\":\"" + requestId"\"}";
//    String jsonStr = JSONObject.toJSONString(jsonObject);
//    System.err.println(jsonStr);
//    String signStr = sign(jsonStr);
//    System.err.println(signStr);
//    TestTax.post(signStr, "https://einvoice.cloud.alipay.com/api/invoice/queryByTime");
//  }
//
//  private static void verify(String nsrsbh, String fpdm, String fphm, String kprq, String je) {
//    String jsonStr = "{\"nsrsbh\":\"" + nsrsbh + "\",\"fpdm\":\"" + fpdm + "\",\"fphm\":\"" + fphm + "\",\"kprq\":\"" + kprq
//        + "\",\"je\":" + je + "}";
//    TestTax.post(sign(jsonStr), "https://einvoice.cloud.alipay.com/api/invoice/verify");
//  }
//
//  private static void booking(String nsrsbh, String fpdm, String fphm, String fplx, String gfbx_bz, String gfjz_bz, String km_1,
//                              String km_2, String kpyf, String xfbx_bz, String xfjz_bz, String xfsbh) {
//    String jsonStr
//        = "{\"fpdm\": \"" + fpdm + "\",\"fphm\": \"" + fphm + "\",\"fplx\": \"" + fplx + "\",\"gfbx_bz\": \"" + gfbx_bz
//        + "\",\"gfjz_bz\": \"" + gfjz_bz + "\","
//        + "\"" + nsrsbh + "\": \"" + nsrsbh + "\",\"km_1\": \"" + km_1 + "\",\"km_2\": \"" + km_2 + "\",\"kpyf\": \"" + kpyf
//        + "\",\"xfbx_bz\": \"" + xfbx_bz + "\","
//        + "\"xfjz_bz\": \"" + xfjz_bz + "\",\"xfsbh\": \"" + xfsbh + "\"}";
//    String sign = sign(jsonStr);
//    TestTax.post(sign, "https://einvoice.cloud.alipay.com/api/invoice/booking");
//  }
//
//}
